﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.StructureDiagram
{
    public partial class StructureDiagramAdd : BasePage
    {
        public string picpath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup=" + (int)PubEnums.RoleGroup.客户), ddlCompanyID, "ID", "CompanyName");//绑定公司
                if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.客户.ToString())
                {
                    ddlCompanyID.SelectedValue = SessionHelper.UserCompanyID.ToString();
                    ddlCompanyID.Enabled = false;
                }

                //编辑
                if (Request.QueryString["id"] != null)
                {
                    this.lblPageName.Text = "结构图修改";
                    lblViewPic.Visible = true;
                    string strId = Request.QueryString["id"].ToString();
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        dataBind(id);
                    }
                }
                else
                {
                    this.lblPageName.Text = "结构图添加";
                    lblViewPic.Visible = false;
                }
            }
        }

        private void dataBind(int id)
        {
            Entity.tbStructureDiagramEntity entity = new ITOMMS.Entity.tbStructureDiagramEntity();
            entity = entity.GetById(id);
            if (entity != null)
            {
                ViewState["Entity"] = entity;
                txtSDName.Text = entity.SDName;
                ddlCompanyID.SelectedValue = entity.customerID.ToString();
                ddlType.Items.FindByText(entity.SDType);
                txtDescription.Text = entity.discription;
                if (!string.IsNullOrEmpty(entity.picpath))
                {
                    picpath = "../UpLoadFiles/" + entity.picpath;
                  
                    //HyperLinkoldPic.NavigateUrl = "StructureDiagramView.aspx?picname=" + entity.picpath;
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ddlType.SelectedValue))
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('请选择结构图类型。',300,180,'提示',null);</script>");
                return;
            }

            if (string.IsNullOrEmpty(this.txtSDName.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('请输入结构图名称。',300,180,'提示',null);</script>");
                return;
            }

            if (!FileUpload1.HasFile)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>ymPrompt.alert('请输入需要上传的图片路径。',300,180,'提示',null);</script>");
                return;
            }


            try
            {
                Entity.tbStructureDiagramEntity entity = new ITOMMS.Entity.tbStructureDiagramEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbStructureDiagramEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }
                entity.SDName = txtSDName.Text;
                entity.customerID = int.Parse(ddlCompanyID.SelectedValue);
                //entity.customerID = SessionHelper.UserCompanyID;
                entity.SDType = ddlType.SelectedItem.ToString();
                entity.discription = txtDescription.Text;

                if (FileUpload1.FileName != string.Empty && FileUpload1.HasFile)
                {
                    string imageFile = string.Empty;
                    imageFile = SaveImgToServer(FileUpload1);
                    entity.picpath = imageFile;
                    //newPreview.InnerHtml = "    <img  src='../UpLoadFiles/" + entity.XImageURL + "'/> ";
                }



                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Now;

                entity.SaveOrUpdate();
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }


        private string SaveImgToServer(FileUpload imageUpload)
        {
            string rs = string.Empty;
            try
            {
                if (imageUpload.PostedFile.FileName == "")
                {
                    return rs;
                }

                string fileName = Guid.NewGuid().ToString();


                string filePath = imageUpload.PostedFile.FileName.ToLower();
                string fileEx = filePath.Substring(filePath.LastIndexOf(".") + 1);
                if (!System.IO.Directory.Exists(Server.MapPath(@"~/UpLoadFiles\")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(@"~/UpLoadFiles\"));
                }


                //大小图
                string Samllserverpath1 = Server.MapPath(@"~/UpLoadFiles\") + fileName + "." + fileEx;
                //if (imageUpload.PostedFile.InputStream.Length > 2097152)
                //{
                //    MessageboxInfo.MessageBox(this, MessageType.ERROR, "图片太大");
                //    return "";
                //}


                ////判断图片格式
                //if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif" || fileEx == "jpeg" || fileEx == "png")
                //{

                    //System.Drawing.Image image, newimage;
                    //保存大图

                    imageUpload.PostedFile.SaveAs(Samllserverpath1);

                    //lblImage.Text = "<img src='uploadpic/" + xfilename + "." + fileEx + "' width='100' height='80'/>";
                    rs = fileName + "." + fileEx;

                //}
                //else
                //{
                //    MessageboxInfo.MessageBox(this, MessageType.ERROR, "图片格式不正确");
                //}

            }
            catch
            {
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "图片上传失败");
            }
            return rs;
        }


    }
}
